package game.zqll.systems;

import com.artemis.Aspect;
import com.artemis.systems.IteratingSystem;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.math.Rectangle;
import game.zqll.PluginFileHandle;
import game.zqll.components.DropComp;
import game.zqll.components.PosComp;
import game.zqll.components.SizeComp;

public class CollisionSystem extends IteratingSystem {

    private PlayerSystem playerSystem;
    private ModGuiSystem modGuiSystem;

    private Rectangle bucketRect = new Rectangle();
    private Rectangle dropRect = new Rectangle();
    private Sound dropSound;
    private Music music;

    public CollisionSystem() {
        super(Aspect.all(DropComp.class));
    }

    @Override
    protected void initialize() {
        dropSound = Gdx.audio.newSound(new PluginFileHandle("drop.mp3"));
        music = Gdx.audio.newMusic(new PluginFileHandle("music.mp3"));
        music.play();
        music.setLooping(true);
    }

    @Override
    protected void begin() {
        PosComp posComp = Mappers.pos.get(playerSystem.get());
        SizeComp sizeComp = Mappers.size.get(playerSystem.get());
        bucketRect.set(posComp.x, posComp.y, sizeComp.width, sizeComp.height);
    }

    @Override
    protected void process(int entityId) {
        PosComp posComp = Mappers.pos.get(entityId);
        SizeComp sizeComp = Mappers.size.get(entityId);
        dropRect.set(posComp.x, posComp.y, sizeComp.width, sizeComp.height);
        if (bucketRect.overlaps(dropRect)) {
            world.delete(entityId);
            dropSound.play();
            modGuiSystem.incrScore();
        }
    }

    @Override
    protected void dispose() {
        dropSound.dispose();
        music.dispose();
    }
}
